package yunwo.cn.yzstation.view.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;
import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.api.ISettingView;
import yunwo.cn.yzstation.base.Yzapp;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.api.okapi.RegistCall;
import yunwo.cn.yzstation.base.MyBaseActivity;
import yunwo.cn.yzstation.bean.RegistBean;
import yunwo.cn.yzstation.other.PickDialog;
import yunwo.cn.yzstation.other.ShowAnimationDialogUtil;
import yunwo.cn.yzstation.presenter.SettingActivityPersenter;
import yunwo.cn.yzstation.utils.StringUtil;

/**
 * Created by lao on 2017/9/1.
 */
public class SettingActivity extends MyBaseActivity implements View.OnClickListener,ISettingView {

    private LinearLayout mNick;

    private TextView mCancel;
    private TextView mConfirm;
    private TextView mUsername;
    private TextView mUsername1;
    private EditText mEt;
    private LinearLayout mUserphone;
    private Intent mIntent;
    private LinearLayout mPasswd;
    private SharedPreferences mInformation;
    private CircleImageView mPic;
    private SettingActivityPersenter mSettingActivityPersenter;
    public static final int SELECT_USER_PHOTO_PIC = 301;
    private static final int CUT_SELECT_USER_PHOTO_PIC = 401;
    private static final int CUT_USER_PHOTO = 100;
    public static final int USER_PHOTO = 0;
    private static final int CAMERA_TASK_CODE = 2;
    private TextView mPhone;
    private PickDialog mPickDialog;
    private Button mExit;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initNativeData();
        mSettingActivityPersenter = new SettingActivityPersenter(this,this);
        mSettingActivityPersenter.loadInfos();
    }

    private void initNativeData() {
        mUsername.setText(Yzapp.getInstance().getUserName());
        mUsername1.setText(Yzapp.getInstance().getUserName());
        mPhone.setText(Yzapp.getInstance().getmobile());
        Intent intent = new Intent();
        intent.putExtra("username", mUsername.getText().toString());
        intent.putExtra("userphone", mPhone.getText().toString());
        intent.putExtra("pic", mInformation.getString("LocalPic", ""));
        setResult(98,intent);
    }

    private void initView() {
        mInformation = getSharedPreferences("information", Context.MODE_PRIVATE);
        mExit = (Button) findViewById(R.id.exit);
        mPhone = (TextView) findViewById(R.id.phone);
        mNick = (LinearLayout) findViewById(R.id.nick);
        mPic = (CircleImageView) findViewById(R.id.pic);
        mPasswd = (LinearLayout) findViewById(R.id.passwd);
        mUsername = (TextView) findViewById(R.id.username);
        mUsername1 = (TextView) findViewById(R.id.username1);
        mUserphone = (LinearLayout) findViewById(R.id.userphone);
        mPic.setOnClickListener(this);
        mExit.setOnClickListener(this);
        mNick.setOnClickListener(this);
        mPasswd.setOnClickListener(this);
        mUserphone.setOnClickListener(this);

    }

    @Override
    protected String settiltile() {
        return "设置";
    }

    @Override
    protected int getLayoutRsid() {
        return R.layout.activity_setting;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.pic:
                initSelectImageDialogView(USER_PHOTO,SELECT_USER_PHOTO_PIC);
                break;
            case R.id.nick:
                initnick();
                break;
            case R.id.userphone:
                mIntent = new Intent(SettingActivity.this, MOdifyphoneActivity.class);
                startActivity(mIntent);
                break;
            case R.id.passwd:
                mIntent = new Intent(SettingActivity.this, ModifyPasswdActivity.class);
                startActivity(mIntent);
                break;
            case R.id.exit:

                mSettingActivityPersenter.Logout();

                break;
        }
    }


    private void initnick() {


        mPickDialog = ShowAnimationDialogUtil.showDialog(SettingActivity.this,
                R.layout.dalog_nick, Gravity.CENTER, false, false);
        mPickDialog.setCancelable(false);
        View view = mPickDialog.getView();
        mCancel = (TextView) view.findViewById(R.id.cancel);
        mConfirm = (TextView) view.findViewById(R.id.confirm);
        mEt = (EditText) view.findViewById(R.id.et);

        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPickDialog.dismiss();
            }
        });
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!mEt.getText().toString().trim().equals("")) {
                    mSettingActivityPersenter.upDateNick(mEt.getText().toString().trim());
                } else {
                    Toast.makeText(SettingActivity.this, "输入新的昵称", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }


    public void initSelectImageDialogView(final int photoId, final int selectPhotoId) {
//        if (isAllowedChangeData && mPresenter.isAllowedChangeData()) {
        final PickDialog selectImageDialog = ShowAnimationDialogUtil.showDialog(this,
                R.layout.photo_select_dialog);
        View view = selectImageDialog.getView();
        TextView selectcCancelTV = (TextView) view.findViewById(R.id.cancelTV);
        TextView capturePicTV = (TextView) view.findViewById(R.id.capturePicTV);
        TextView selectPicTV = (TextView) view.findViewById(R.id.selectPicTV);
        selectcCancelTV.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                selectImageDialog.dismiss();
            }
        });
        capturePicTV.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(SettingActivity.this, Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {
                    //申请CAMERA权限
                    ActivityCompat.requestPermissions(SettingActivity.this, new String[]{Manifest.permission.CAMERA},
                            CAMERA_TASK_CODE);
                }else {
                    mSettingActivityPersenter.capturePicture(photoId);
                }
                selectImageDialog.dismiss();
            }
        });
        selectPicTV.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(SettingActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
                    //申请WRITE_EXTERNAL_STORAGE权限
                    ActivityCompat.requestPermissions(SettingActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            selectPhotoId);
                } else {
//                    mPresenter.selectImage(selectPhotoId);
                    mSettingActivityPersenter.selectImage(selectPhotoId);
                }
                selectImageDialog.dismiss();
            }
        });
//        }
    }

    @Override
    public void startMyActivityForResult(Intent intent, int photoId) {
        startActivityForResult(intent, photoId);
    }

    @Override
    public void showIconImage(String path) {
        Log.d("showIconImage", "showIconImage: path:" + path);

        if (!StringUtil.isEmpty(path)) {
            Picasso.with(SettingActivity.this).load(path).into(mPic);
        }
    }

    @Override
    public void UpDateNickSuccess(String nick) {
        mUsername.setText(nick);
        mUsername1.setText(nick);
        mPickDialog.dismiss();
    }

    @Override
    public void LogoutSuccess() {
        SharedPreferences.Editor editor = Yzapp.getInstance().getinfoSPEditor();
        editor.clear();
        editor.commit();
        mIntent = new Intent(SettingActivity.this, LoginActivity.class);
        startActivity(mIntent);
        finish();
    }

    @Override
    public void UpDateNickfailure() {
        Toast.makeText(SettingActivity.this, "修改昵称失败,服务器异常", Toast.LENGTH_SHORT).show();
        mPickDialog.dismiss();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("xxxxxxx", "requestCode:" + requestCode + ",resultCode:" + resultCode);
        String nickName = mUsername1.getText().toString().trim();
        switch (requestCode) {
            case USER_PHOTO:

                //拍照
                mSettingActivityPersenter.setCapturePhoto(USER_PHOTO, CUT_USER_PHOTO);
                break;
            case SELECT_USER_PHOTO_PIC:
                if (data == null)
                    return;
                //选择头像
                mSettingActivityPersenter.setSelectPhoto(data, CUT_SELECT_USER_PHOTO_PIC);
                break;
            case CUT_SELECT_USER_PHOTO_PIC:
                if (data == null)
                    return;
                Bundle extras = data.getExtras();
                if (extras != null) {
                    String path = extras.getString(CutPicActivity.INTENT_PATH, "");
                    mSettingActivityPersenter.setCutImageView(path, USER_PHOTO, SELECT_USER_PHOTO_PIC, false, nickName);
                }
                break;
            case CUT_USER_PHOTO:
                if (data == null)
                    return;
                extras = data.getExtras();
                if (extras != null) {
                    String path = extras.getString(CutPicActivity.INTENT_PATH, "");
                    mSettingActivityPersenter.setCutImageView(path, USER_PHOTO, SELECT_USER_PHOTO_PIC, true, nickName);
                }
                break;

        }
    }
}
